package com.ksd.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.course.entity.AdminCourseComment;
import com.ksd.modules.course.mapper.AdminCourseCommentMapper;
import com.ksd.modules.course.po.AdminCourseCommentSavePO;
import com.ksd.modules.course.po.AdminCourseCommentSearchPO;
import com.ksd.modules.course.service.IAdminCourseCommentReplyService;
import com.ksd.modules.course.service.IAdminCourseCommentService;
import com.ksd.modules.course.vo.AdminCourseCommentReplyVo;
import com.ksd.modules.course.vo.AdminCourseCommentVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminCourseCommentServiceImpl extends BaseServiceImpl<AdminCourseCommentMapper, AdminCourseComment> implements IAdminCourseCommentService {

    @Resource
    private IAdminCourseCommentReplyService adminCourseCommentReplyService;

    /**
     * 查询列表
     * @param adminCourseCommentSearchPO
     * @return
     */
    public Page<AdminCourseCommentVo> findAdminCourseComments(AdminCourseCommentSearchPO adminCourseCommentSearchPO){
        Page<AdminCourseComment> page = new Page<>(adminCourseCommentSearchPO.getPageNo(),adminCourseCommentSearchPO.getPageSize());
        LambdaQueryWrapper<AdminCourseComment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminCourseComment::getSystemId,SystemIdThrealLocal.get().getSystemId());
        // 资源id
        lambdaQueryWrapper.eq(Objects.nonNull(adminCourseCommentSearchPO.getOpid()), AdminCourseComment::getOpid,adminCourseCommentSearchPO.getOpid());
        // 用户唯一标识
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminCourseCommentSearchPO.getUuid()), AdminCourseComment::getUuid,adminCourseCommentSearchPO.getUuid());
        lambdaQueryWrapper.eq(Objects.nonNull(adminCourseCommentSearchPO.getStatus()), AdminCourseComment::getStatus,adminCourseCommentSearchPO.getStatus());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminCourseCommentSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminCourseComment::getUsername, adminCourseCommentSearchPO.getKeyword())
                    .or().like(AdminCourseComment::getUuid, adminCourseCommentSearchPO.getKeyword())
                    .or().like(AdminCourseComment::getTitle, adminCourseCommentSearchPO.getKeyword())

            );
        }
        // 排序
        lambdaQueryWrapper.orderByDesc(AdminCourseComment::getCreateTime);
        Page<AdminCourseComment> adminCoursePage = this.page(page, lambdaQueryWrapper);
        Page<AdminCourseCommentVo> adminCourseVoPage = tranferPageBo(adminCoursePage, AdminCourseCommentVo.class);
        List<AdminCourseCommentVo> records = adminCourseVoPage.getRecords();
        // 查询所有的第一页的子回复
        if(CollectionUtils.isNotEmpty(records)){
            for (AdminCourseCommentVo record : records) {
                AdminCourseCommentSearchPO adminCourseCommentSearchPO1 = new AdminCourseCommentSearchPO();
                adminCourseCommentSearchPO1.setPageNo(1);
                adminCourseCommentSearchPO1.setPageSize(3);
                adminCourseCommentSearchPO1.setParentId(record.getId());
                Page<AdminCourseCommentReplyVo> childrenReplysPage = adminCourseCommentReplyService.findAdminCourseCommentReplys(adminCourseCommentSearchPO1);
                record.setReplyNum(childrenReplysPage.getTotal());
                record.setChildren(childrenReplysPage);
            }
        }
        return adminCourseVoPage;
    }

    /**
     * 保存和更新
     * @param adminCourseCommentSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminCourseCommentVo saveUpdateAdminCourseComment(AdminCourseCommentSavePO adminCourseCommentSavePO){
        AdminCourseComment adminCourse = tranferBo(adminCourseCommentSavePO, AdminCourseComment.class);
        adminCourse.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminCourse.setSystemName(SystemIdThrealLocal.get().getSystemName());
        this.saveOrUpdate(adminCourse);
        AdminCourseCommentVo adminCourseVo = tranferBo(adminCourse, AdminCourseCommentVo.class);
        return adminCourseVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminCourseCommentVo getAdminCourseCommentById(Long id){
        AdminCourseComment adminCourse = this.getById(id);
        AdminCourseCommentVo adminCourseVo = tranferBo(adminCourse, AdminCourseCommentVo.class);
        return adminCourseVo;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminCourseCommentById(Long id){
        boolean b = this.removeById(id);
        if(b){

        }
        return b;
    }


    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminCourseComment(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        if(b){
            adminCourseCommentReplyService.updateStatusByParentId(id,column,value);
        }
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        if(b){
            adminCourseCommentReplyService.updateStatusByParentId(id,column,value);
        }
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchDowns(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminCourseComment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.UN_STATUS.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if(b){
            adminCourseCommentReplyService.batchUpdateStatusByParentIds(ids,"status", StatusEnums.UN_STATUS.getCode());
        }
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchUps(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminCourseComment> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.STATUS.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        if(b){
            adminCourseCommentReplyService.batchUpdateStatusByParentIds(ids,"status", StatusEnums.STATUS.getCode());
        }
        return b;
    }
}
